var api = require('./api')
var app = getApp()

function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

// function formatTime2(date) {
//   console.log(date);
//   var year = date.getFullYear()
//   var month = date.getMonth() + 1
//   var day = date.getDate()

//   return [year, month, day].map(formatNumber).join('-')
// }

function formatTime2 (value){
  // if(value.length == 8){
  //     return value.substring(0, 4) + "-" + value.substring(4, 6) + "-" + value.substring(6, 8);
  // } else 
  if(value != null){
      //return value.substring(0, 4) + "-" + value.substring(4, 6);
      return value.substring(0, 4) + "-" + value.substring(4, 6) + "-" + value.substring(6, 8);
  } else {
  return value;
  }
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/**
 * 封封微信的的request
 */
function request(url, data = {}, method = 'GET') {
  return new Promise(function (resolve, reject) {
    var token = wx.getStorageSync('token')
    // if (token == null || token == '' || token == undefined) {
    //   // 切换到登录页面
    //   wx.navigateTo({
    //     url: '/pages/login/index'
    //   })
    // }

    // if(wx.getStorageSync('bindphone')==true){
    //    console.log(123)
    //    //没有绑定手机号，跳转绑定
    //    wx.navigateTo({
    //     url: '/pages/login/bindcode/index'
    //   })
    // }
    wx.request({
      url: url,
      data: data,
      method: method,
      header: {
        //'Content-Type': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded',
        'X-Litemall-Token': token
      },
      success: function (res) {
        if (res.statusCode == 200) {
          if (res.data.errno == 501) {
            // 清除登录相关内容
            try {
              wx.removeStorageSync('userInfo')
              wx.removeStorageSync('token')
            } catch (e) {
              // Do something when catch error
            }
            // 切换到登录页面
            wx.navigateTo({
              url: '/pages/login/index'
            })
          } else {
            resolve(res.data)
          }
        } else {
          reject(res.errMsg)
        }
      },
      fail: function (err) {
        reject(err)
      }
    })
  })
}

function requestlogin(url, data = {}, method = 'GET') {
  var token = null;
  // if(wx.getStorageInfoSync("token")!=null){
  //   token=wx.getStorageInfoSync("token")
  // }
  return new Promise(function (resolve, reject) {
    wx.request({
      url: url,
      data: data,
      method: method,
      header: {
        // 'Content-Type': 'application/json',
        //'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
        'X-Litemall-Token': wx.getStorageSync("token")
      },
      success: function (res) {
        if (res.statusCode == 200) {
          if (res.data.errno == 501) {
            // 清除登录相关内容
            try {
              wx.removeStorageSync('userInfo')
              wx.removeStorageSync('token')
            } catch (e) {
              // Do something when catch error
            }
            // 切换到登录页面
            wx.navigateTo({
              url: '/pages/login/index'
            })
          } else {
            resolve(res.data)
          }
        } else {
          reject(res.errMsg)
        }
      },
      fail: function (err) {
        reject(err)
      }
    })
  })
}

function redirect(url) {
  //判断页面是否需要登录
  if (false) {
    wx.redirectTo({
      url: '/pages/auth/login/login'
    })
    return false
  } else {
    wx.redirectTo({
      url: url
    })
  }
}

function showErrorToast(msg) {
  wx.showToast({
    title: msg,
    image: '/static/images/icon_error.png'
  })
}

export { formatTime, request, redirect, showErrorToast, requestlogin,formatTime2 }
